﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace BusinessRule
{
    public class JsonHelper
    {
        #region dataTable转换成Json格式
        /// <summary>      
        /// dataTable转换成Json格式      
        /// </summary>      
        /// <param name="dt"></param>      
        /// <returns></returns>      
        public static string DataTableToJson(DataTable dt)
        {
            StringBuilder jsonBuilder = new StringBuilder();
            jsonBuilder.Append("{\"");
            jsonBuilder.Append(dt.TableName.ToString());
            jsonBuilder.Append("\":[");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                jsonBuilder.Append("{");
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    jsonBuilder.Append("\"");
                    jsonBuilder.Append(dt.Columns[j].ColumnName);
                    jsonBuilder.Append("\":\"");
                    jsonBuilder.Append(dt.Rows[i][j].ToString());
                    jsonBuilder.Append("\",");
                }
                jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                jsonBuilder.Append("},");
            }
            jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
            jsonBuilder.Append("]");
            jsonBuilder.Append("}");
            return jsonBuilder.ToString();
        }
        /*返回的格式
          { "programmers": [
          { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
          { "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" },
          { "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
          ]}
         */
        #endregion dataTable转换成Json格式
        #region dataset转json
        /// <summary>
        /// dataset
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static string DataSetToJson(DataSet ds)
        {
            StringBuilder jsonBuilder = new StringBuilder();
            jsonBuilder.Append("{");
            foreach (DataTable dt in ds.Tables)
            {
                jsonBuilder.Append("\"");
                jsonBuilder.Append(dt.TableName.ToString());
                jsonBuilder.Append("\":[");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    jsonBuilder.Append("{");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        jsonBuilder.Append("\"");
                        jsonBuilder.Append(dt.Columns[j].ColumnName);
                        float f;
                        if (float.TryParse(dt.Rows[i][j].ToString(), out f))
                        {
                            jsonBuilder.Append("\":");
                            jsonBuilder.Append(dt.Rows[i][j].ToString());
                            jsonBuilder.Append(",");
                        }
                        else
                        {
                            jsonBuilder.Append("\":\"");
                            jsonBuilder.Append(dt.Rows[i][j].ToString());
                            jsonBuilder.Append("\",");
                        }
                    }
                    jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                    jsonBuilder.Append("},");
                }
                jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                jsonBuilder.Append("],");
            }
            jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
            jsonBuilder.Append("}");
            return jsonBuilder.ToString();
        }
        /*返回的格式
             { "programmers": [
　　        { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
　　        { "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" },
　　        { "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
　　        ],
　　        "authors": [
　　        { "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
　　        { "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
　　        { "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
　　        ],
　　        "musicians": [
　　        { "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
　　        { "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
　　        ] }
         */
        #endregion
    }
}
